<?php 
	include 'db.php';	
	//select current user shopping cart and get total
	
	function printCart($discountValue){
		$_SESSION['receiptDate'] = date('F j, Y');
		$pdfHeading = '<style>';
		$pdfHeading .= '.customerWrap {  float:left; width: 200px; }';
		$pdfHeading .= '.align-center { text-align: center;	}';		
		$pdfHeading .= '.align-left { text-align: left;	}';
		$pdfHeading .= '.align-right { text-align: right;	}';
		$pdfHeading .= '.detailWrap { text-align: right; width: 200px;  float: right;	}';
		$pdfHeading .= '.clear { clear: both; }';
		$pdfHeading .= ' table.table tbody tr td { text-align: center; font-size: 11px;	}';
		$pdfHeading .= '</style>'; 
		$pdfHeading .= '<h2 class="align-center"><strong>Proforma Invoice</strong></h2>';
		$pdfHeading .= '<table class="heading">';
		$pdfHeading .= '<tr>';
		$pdfHeading .= '<td class="align-left">' .$_SESSION['name']. '<br />'.$_SESSION['jobtitle'].'<br />'.$_SESSION['location'].'<br />'.$_SESSION['PO'].'<br />Dubai, UAE<br />Tel - '.$_SESSION['number'].'<br />Email - '.$_SESSION['email'].'  </td>';
		$pdfHeading .= '	<td class="align-right">'.$_SESSION['receiptDate'].' <br /><span>P/INV -'.$_SESSION['proformaID'].'</span>  </td>';
		$pdfHeading .= '</tr><tr><td>&nbsp;</td><td>&nbsp;</td></tr>';		 
		$pdfHeading .= '</table>';
          $pdfOutput = ' <p>Dear Sir/Ma\'am; </p>';
          $pdfOutput .= '<p>We are pleased to offer you our pricing for PHILIPS LED for the above mentioned project.</p>';
		
		//$id_session = session_id();			
		$totalVal = 0;
		$totalCost = 0; 
		$sql = "SELECT *
				FROM items";
	 
		//open the table and print a header row
			$pdfOutput .= '<table class="table table-bordered">';
			$pdfOutput .= '<thead><tr>';
			$pdfOutput .= ' <th>PH Code</th>';
			$pdfOutput .= ' <th>Item Description</th>';
			$pdfOutput .= '<th>Quantity</th>';
			$pdfOutput .= '<th>Unit Price</th>';
			$pdfOutput .= '<th>Total Value</th>';
			$pdfOutput .= '</tr></thead>';
			$pdfOutput .= '<tbody>';		
		//write for html display
			$htmlOutput = '<table class="table table-bordered">';
			$htmlOutput .= '<thead><tr>';
			$htmlOutput .= ' <th>PH Code</th>';
			$htmlOutput .= ' <th>Item Description</th>';
			$htmlOutput .= '<th>Quantity</th>';
			$htmlOutput .= '<th>Unit Price</th>';
			$htmlOutput .= '<th>Total Value</th>';
			$htmlOutput .= '</tr></thead>';
			$htmlOutput .= '<tbody>';	
			
			
		if ($result=mysql_query($sql)) {
		  while ($row=mysql_fetch_array($result)) {	
		    if($row['itemId'] == @$_SESSION['item_'.$row['itemId']]){
		  //write for pdf	 
			$pdfOutput .= '<tr>';
			$pdfOutput .= '<td>'.$row['phCode'].'</td>';
			$pdfOutput .= '<td>'.$row['itemName'].' '.$row['itemDesc'].'</td>';
			$pdfOutput .= '<td>'.@$_SESSION['item_'.$row['itemId'].'_qty'].'</td>';
			$pdfOutput .= '<td>'.$row['itemPrice'].'</td>';
			$totalVal = @$_SESSION['item_'.$row['itemId'].'_qty'] * $row['itemPrice'];
			$pdfOutput .= '<td>' .$totalVal. '</td>';
			$pdfOutput .= '</tr>';
		//write for html
			$htmlOutput .= '<tr>';
			$htmlOutput .= '<td>'.$row['phCode'].'</td>';
			$htmlOutput .= '<td>'.$row['itemName'].' '.$row['itemDesc'].'</td>';
			$htmlOutput .= '<td>'.@$_SESSION['item_'.$row['itemId'].'_qty'].'</td>';
			$htmlOutput .= '<td>'.$row['itemPrice'].'</td>';
			$htmlOutput .= '<td>' .$totalVal. '</td>';
			$htmlOutput .= '</tr>';
			//increment values for totalCost
			$totalCost += @$_SESSION['item_'.$row['itemId'].'_qty'] * $row["itemPrice"];
			}
		  } 
		  //write for pdf
			$pdfOutput .= '<tr class="nonprint">';
			$pdfOutput .= '<td colspan="3">&nbsp;</td>';		
			$pdfOutput .= '<td><strong>Discount (%)</strong></td>';
			$pdfOutput .= '<td>'.$discountValue.' %</td>'; 
			$pdfOutput .= '</tr>';	
		  //write for html			
			$htmlOutput .= '<tr class="nonprint">';
			$htmlOutput .= '<td colspan="3">&nbsp;</td>';		
			$htmlOutput .= '<td><strong>Discount (%)</strong></td>';
			$htmlOutput .= '<td>'; 
			$htmlOutput .= '	<form action="print.php" method="POST">';
			$htmlOutput .= '		<input type="hidden" id="isDiscounted" name="isDiscounted" value="1" />	';
			$htmlOutput .= '  	 <input class="disc col_1" type="text" name="discount" id="discount" maxlength="2" value="'.$discountValue.'">';
			$htmlOutput .= '	  <button type="submit" class="btn btn-mini">deduct</button>';
			$htmlOutput .= '	</form>';
			$htmlOutput .= ' </td>'; 
			$htmlOutput .= '</tr>';	
		
		//write for pdf		
			$pdfOutput .= '<tr>';			
			$pdfOutput .= '<td colspan="3">&nbsp;</td>';
			$pdfOutput .= '<td><strong>TOTAL AED</strong></td>';			
			$pdfOutput .= '<td> '.$totalCost.' </td>';
			$pdfOutput .= '</tr>';
		//write for html
			$htmlOutput .= '<tr>';			
			$htmlOutput .= '<td colspan="3">&nbsp;</td>';
			$htmlOutput .= '<td><strong>TOTAL AED</strong></td>';			
			$htmlOutput .= '<td> '.$totalCost.' </td>';		
			$htmlOutput .= '</tr>';
			
			$_SESSION['totalCost'] = $totalCost;
			
			if($_SESSION['discounted'] == 'true'){
				$percentage =  $discountValue / 100;
				$percent = $percentage * $totalCost;
				$discPrice = $totalCost - $percent;
				$discPrice = round($discPrice, 2);	
				$_SESSION['discPrice'] = $discPrice;	
			//write for pdf		
				$pdfOutput .= '<tr>';
				$pdfOutput .= '<td colspan="3">&nbsp;</td>';
				$pdfOutput .= '<td><strong>Discounted Price</strong></td>';
				$pdfOutput .= '<td><strong>'.$discPrice.'</strong></td>';
				$pdfOutput .= '</tr>';
			//write for html
				$htmlOutput .= '<tr>';
				$htmlOutput .= '<td colspan="3">&nbsp;</td>';
				$htmlOutput .= '<td><strong>Discounted Price</strong></td>';
				$htmlOutput .= '<td><strong>'.$discPrice.'</strong></td>';
				$htmlOutput .= '</tr>';
			}
			//write for pdf	
			$pdfOutput .= '<tr><td colspan="5"></td></tr><tr><td colspan="5"></td></tr></tbody>';
			$pdfOutput .= '</table>';
            $pdfOutput .= '<br /><br />
			';
			//write for html
			$htmlOutput .= '</tbody>';
			$htmlOutput .= '</table>'; 
		 } else {
		  echo mysql_error();
		}
		$cartID = $row['cartID'];
		$_SESSION['pdfHeading'] = $pdfHeading;
		$_SESSION['pdfOutput'] = $pdfOutput;
		echo $htmlOutput;
	}
?>